package manage

import (
	"lingc/models"
	"lingc/services"
	"lingc/utils" // 导入 common 包
	"log"
	"strings"

	"github.com/gin-gonic/gin"
)

// PageChatAutoReply 获取所有用户
func PageChatAutoReply(c *gin.Context) {

	var dataRequest struct {
		Keywords string `json:"keywords" form:"keywords"`
		Page     int    `json:"page" form:"page"`
		PageSize int    `json:"pageSize" form:"pageSize"` // Exported field
	}
	if err := c.ShouldBind(&dataRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	where := ""
	if dataRequest.Keywords != "" {
		keyword := "'%" + dataRequest.Keywords + "%'"
		where += " `content` like " + keyword
	}
	list, page, total, err := services.NewChatAutoReplyService(utils.MysqlClient).PageChatAutoReplyList(where, dataRequest.Page, dataRequest.PageSize)
	if err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"list": list, "page": page, "total": total})
}

// GetChatAutoReply 获取
func GetChatAutoReply(c *gin.Context) {

	var dataRequest struct {
		Keyword string `json:"keyword" form:"keyword"`
	}
	where := ""
	if err := c.ShouldBind(&dataRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	if dataRequest.Keyword != "" {
		keyword := "%" + dataRequest.Keyword + "%"
		where += " `content` like " + keyword
	}
	user, err := services.NewChatAutoReplyService(utils.MysqlClient).GetChatAutoReply(where)
	if err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"data": user})
}

// CreateUser 创建用户
func CreateChatAutoReply(c *gin.Context) {

	var createRequest struct {
		AdminID uint   `json:"admin_id" form:"admin_id"`
		Title   string `json:"title" form:"title"`
		MsgType uint8  `json:"msg_type" form:"msg_type"`
		Content string `json:"content" form:"content"`
		Type    string `json:"type" form:"type"`
		Status  uint8  `json:"status" form:"status"`
		Sort    uint   `json:"sort" form:"sort"`
	}
	if err := c.ShouldBindJSON(&createRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	data := &models.ChatAutoReplyCreate{
		AdminID: createRequest.AdminID,
		Title:   createRequest.Title,
		MsgType: createRequest.MsgType,
		Content: createRequest.Content,
		Type:    createRequest.Type,
		Status:  createRequest.Status,
		Sort:    createRequest.Sort,
	}
	log.Printf("data: %+v", data)
	data, err := services.NewChatAutoReplyService(utils.MysqlClient).CreateChatAutoReply(data)
	if err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"data": data})
}

// UpdateUser 更新用户
func UpdateChatAutoReply(c *gin.Context) {
	var createRequest struct {
		ID      uint   `json:"id" form:"id"`
		AdminID uint   `json:"admin_id" form:"admin_id"`
		Title   string `json:"title" form:"title"`
		MsgType uint8  `json:"msg_type" form:"msg_type"`
		Content string `json:"content" form:"content"`
		Type    string `json:"type" form:"type"`
		Status  uint8  `json:"status" form:"status"`
		Sort    uint   `json:"sort" form:"sort"`
	}
	if err := c.ShouldBindJSON(&createRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	data := &models.ChatAutoReplyUpdate{
		ID:      createRequest.ID,
		AdminID: createRequest.AdminID,
		Title:   createRequest.Title,
		MsgType: createRequest.MsgType,
		Content: createRequest.Content,
		Type:    createRequest.Type,
		Status:  createRequest.Status,
		Sort:    createRequest.Sort,
	}
	data, err := services.NewChatAutoReplyService(utils.MysqlClient).UpdateChatAutoReply(data)
	if err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"data": data})
}

// DeleteUser 删除用户
func DeleteChatAutoReply(c *gin.Context) {
	var createRequest struct {
		ID  uint   `json:"id" form:"id"`
		IDs string `json:"ids" form:"ids"`
	}
	if err := c.ShouldBindJSON(&createRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	if createRequest.ID == 0 && len(createRequest.IDs) == 0 {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	if createRequest.ID != 0 {
		if err := services.NewChatAutoReplyService(utils.MysqlClient).DeleteChatAutoReply(createRequest.ID); err != nil {
			utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
			return
		}
	}
	if len(createRequest.IDs) == 0 {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	idStrs := strings.Split(createRequest.IDs, ",")
	var ids []string
	for _, idStr := range idStrs {
		idStr = strings.TrimSpace(idStr)
		if idStr == "" {
			continue
		}
		ids = append(ids, idStr)
	}
	// Convert IDs slice to a format suitable for batch deletion
	if len(ids) == 0 {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "No valid IDs provided"})
		return
	}
	if err := services.NewChatAutoReplyService(utils.MysqlClient).DeleteChatAutoReplys(ids); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	// ... 这里添加删除用户的逻辑 ...
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"msg": "Delete user"})
}
